import { ref, unref } from 'vue'
import { adapter, request } from '@/request'
import { monitorListByGet } from '@/core/api/monitor'
import type { WithTotalSize } from '@/core/interfaces/common'
import type { PlatformEnum } from '@/core/enums/platform'
import type { Pagination, RequestWrapper } from '@/core/interfaces/common'

export interface MonitorParams extends Pagination {
  keyword: string
  startTime?: string
  endTime?: string
  clientType: PlatformEnum
}

export interface monitorWord {
  clientType: PlatformEnum
  createTime: string
  flowHeatRate: number
  id: number
  name: string
  recommendHeatRate: string
}

export default function getMonitorList() {
  const service = adapter<RequestWrapper<WithTotalSize<Record<'monitorWords', monitorWord[]>>>>(request)
  const response = ref<WithTotalSize<Record<'monitorWords', monitorWord[]>>>({
    totalSize: 0,
    monitorWords: []
  })
  const loading = ref(false)

  const fetch = (params: MonitorParams) => {
    const { url, method } = monitorListByGet()
    loading.value = true

    return service({
      config: {
        url,
        method,
        params: unref(params)
      },
      callback: (res) => {
        response.value = res.data.data
      },
      error: (err) => {
        console.log(err)
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
